/*==============================================================================
 * AUTH: Matthew Baum, 9753095
 * DATE: March-05-2012
 * EDIT: March-08-2012
 * DESC: GUI dialog to delete a task
 *==============================================================================*/

package SOEN_Phase_2;

// *** Import SWING GUI libraries *** //
// General window
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JTextField;

// Event handling
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

// *** Main class *** //
public class TaskDeletionDialog extends JFrame implements ActionListener
{
    // Text fields
    JTextField idField = new JTextField(20);

    // Constructor
    public TaskDeletionDialog(TaskTable taskTable, PersonTable personTable, String selected_id)
    {
        this(taskTable, personTable);
        idField.setText(selected_id);
    }
    public TaskDeletionDialog(TaskTable taskTable, PersonTable personTable)
    {
        // Set window title
        setTitle("Delete Task");

        // Set window size
        setSize(450, 300);

        // Set a box layout
        setLayout(new GridLayout(6, 2));

        // Add text fields & labels
        idField.addKeyListener(new NumberFieldValidator());
        add(idField);
        add(new JLabel("ID"));

        // Add "Delete" button
        JButton createButton = new JButton("Delete");
        createButton.addActionListener(new TaskCreationHandler(taskTable, personTable));
        add(createButton);

        // Turn OFF closing
        //setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

        // Set invisible
        setVisible(false);
    }

    // Event handling routine
    @Override
    public void actionPerformed(ActionEvent event)
    {
        // Make this window visible
        this.setVisible(true);
    }

    // EVENT HANDLER: Inner class to create a new Task
    private class TaskCreationHandler implements ActionListener
    {
        // Task table to update
        private TaskTable taskTable;
        private PersonTable personTable;

        // Constructor
        public TaskCreationHandler(TaskTable tTable, PersonTable pTable)
        {
            // Set task table
            taskTable = tTable;
            personTable = pTable;
        }

        // Event handling routine
        @Override
        public void actionPerformed(ActionEvent event)
        {
            // Handle click event
            boolean success = validateFields();

            // IF successful reload Task Table in GUI
            if(success)
            {
                // Reload Task table
                taskTable.fireTableDataChanged();
                personTable.fireTableDataChanged();
                personTable.initFromTaskTable(taskTable);
                
                // Set parent to invisible
                TaskDeletionDialog.this.setVisible(false);
            }

        }

        // Private helper used to validate input
        private boolean validateFields()
        {
            // Get fields
            String idString = idField.getText();

            // Integers to parse int fields
            int ID = -1;

            // Result
            boolean success = false;


            // Validate that fields are filled
            if
            (
                idString.length() > 0
            )
            {
                // Parse int fields
                ID = Integer.parseInt(idString);
                

                // Task to hold result of deletion
                Task result = taskTable.deleteTask(ID);

                // IF deletion successful
                if(result != null)
                {
                    success = true;
                }
                else
                {
                	JOptionPane.showMessageDialog(new JFrame(), "Invalid task ID", "Error", JOptionPane.ERROR_MESSAGE);
                }

            }
            else
            {
            	JOptionPane.showMessageDialog(new JFrame(), "You must enter a task ID to delete", "Error", JOptionPane.ERROR_MESSAGE);
            }
            // Return the result
            return success;
        }
    }
}
